import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'

const processor = unified()
  .use(remarkParse)
  .use(remarkRehype, {allowDangerousHtml: true})
  .use(rehypeStringify)

const app = new Hono()

app.get('/', async (c) => {
  const value = '# Pluto\n\n**Pluto** (minor-planet designation: *13434…'
  const file = await processor.process(value)
  return c.text(String(file))
})

app.post('/markdown', async c => {
  const {message} = await c.req.json()
  const file = await processor.process(message)

  return c.json({
    data: String(file)
  })
})

const port = 3000
console.log(`Server is running on port ${port}`)

serve({
  fetch: app.fetch,
  port
})
